<template>
  <div class="m-life">
    <el-row>
      <el-col :span="14">
        <slider/>
      </el-col>
      <el-col :span="4">
        <div class="m-life-pic"/>
      </el-col>
      <el-col :span="6">
        <div v-if="this.user" class="m-life-login">
          <h4><img
            :src="'data:image/png;base64,'+this.user.headImg"
            :alt="this.user.name">
          </h4>
          <p class="m-life-login-name">欢迎登录！</p>
          <p>
            <nuxt-link to="/userHome">
              <el-button
                round
                size="medium">个人信息
              </el-button>
            </nuxt-link>
          </p>
          <p>
              <el-button
                @click="logout()"
                round
                size="medium">退出登录
              </el-button>
          </p>
        </div>
        <div v-else class="m-life-login">
          <h4><img
            src="@/assets/img/case.png"
            alt=""></h4>
          <p class="m-life-login-name">Hi！你好</p>
          <p>
            <nuxt-link to="/register">
              <el-button
                round
                size="medium">注册
              </el-button>
            </nuxt-link>
          </p>
          <p>
            <nuxt-link to="/login">
              <el-button
                round
                size="medium">立即登录
              </el-button>
            </nuxt-link>
          </p>
        </div>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="7">
        <div class="m-life-hotel"/>

      </el-col>
      <el-col :span="7">
        <div class="m-life-music"/>
      </el-col>
      <el-col :span="4">
        <div class="m-life-coop"/>
      </el-col>
      <el-col :span="6">
        <div class="m-life-downapp">
          <img
            src="@/assets/img/xcx.jpg"
            alt="下载APP">
          <p>微信小程序</p>
          <h4><span class="red">1元起</span><em class="gary">吃喝玩乐</em></h4>
        </div>
      </el-col>
    </el-row>
  </div>
</template>

<script>
  import Slider from './slider.vue'
  import * as homeApi from '@/server/home.js'

  export default {
    components: {
      Slider
    },
    data() {
      return {
        user: null
      }
    },
    methods: {
      getUser: function () {
        var userId = localStorage.getItem("user");
        if (userId != null) {
          homeApi.getUser(userId).then(result => {
            if(result.data.code == 200) {
              this.user = result.data.data;
            }
          })
        }
      },
      logout: function () {
        var userId = localStorage.getItem("user");
        if(userId == null) {
          userId = 0;
        } else {
          homeApi.logout(userId).then(result => {
            localStorage.removeItem("user");
            this.$message({
              message: '注销成功！',
              type: 'success'
            });
            this.$router.push({path:'/login'});
          })
        }
      }
    },
    mounted: function () {
      this.getUser();
    }
  }
</script>


<style lang="scss">
  @import "@/assets/css/index/life.scss";
</style>
